package controllers

import config.{Global, MyFile}
import play.api.mvc.{AbstractController, ControllerComponents, Headers}

import java.io.File
import java.nio.file.Files
import javax.inject.{Inject, Singleton}
import scala.concurrent.ExecutionContext

@Singleton
class ImgController @Inject()(cc: ControllerComponents)(implicit exec: ExecutionContext)
  extends AbstractController(cc) with MyFile {



  def downloadImage(path: String) = Action { implicit request =>
    val name = path.split("/").last
    Ok.sendFile(s"${Global.path}/$path".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> s"attachment; filename=$name",
      CONTENT_TYPE -> "application/x-download"
    )
  }

  def downloadTmpImage(path: String) = Action { implicit request =>
    val name = path.split("/").last
    Ok.sendFile(s"$path".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> s"attachment; filename=$name",
      CONTENT_TYPE -> "application/x-download"
    )
  }


  def getToolsImage(path: String) = Action { implicit request =>
    val file = s"${Global.path}/tmp/$path".toFile
    println(file)
    SendImg(file, request.headers)
  }


  def SendImg(file: File, headers: Headers) = {
    val lastModifiedStr = file.lastModified().toString
    val MimeType = "image/jpg"
    val byteArray = Files.readAllBytes(file.toPath)
    val ifModifiedSinceStr = headers.get(IF_MODIFIED_SINCE)
    if (ifModifiedSinceStr.isDefined && ifModifiedSinceStr.get == lastModifiedStr) {
      NotModified
    } else {
      Ok(byteArray).as(MimeType).withHeaders(
        LAST_MODIFIED -> file.lastModified().toString
      )
    }
  }

  def downloadExample(file: String) = Action { implicit request =>
    Ok.sendFile(s"${Global.path}/example/$file".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> s"attachment; filename=$file",
      CONTENT_TYPE -> "application/x-download"
    )
  }


  def downloadToolsFile(path: String) = Action { implicit request =>
    val name = path.split("/").last
    Ok.sendFile(s"${Global.path}/tmp/$path".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> s"attachment; filename=$name",
      CONTENT_TYPE -> "application/x-download"
    )
  }



}

